package driver;
import core.CalculationException;
import core.DmuList;
import core.GlobalPdmOptimization;
import core.Optimization;
import core.Optimizer;
import core.PdmOptimization;
import facilitator.Bisection;
import facilitator.Bisection_1;
import facilitator.GlobalSolver;
import facilitator.HeuristikSolver;
import facilitator.Tool;
import facilitator.TransitionExplorer;
import java.io.FileNotFoundException;
import java.io.IOException;
import util.Util;
import gui.ChartForm;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Iron {
    static ArrayList<DataSetListItem> datasets = new ArrayList<DataSetListItem>();
    static double[] weightVector = new double[]{21, 34, 11, 13, 15, 22, 32, 21, 34, 11, 13, 15, 22, 32,21, 34, 11, 13, 15, 22, 32, 21, 34, 11, 13, 15, 22, 32, 21, 34, 11, 13, 15, 22, 32, 21, 34, 11};
	public static void graphic(){
		Util.enableWebStart();
		//new MenuFrame();
        //new MainFrame();
        new ChartForm();
	}
	public static void main(String[] args) {
        try{
            //jumper();
            //DoOne("countries_health_remastered", 5);
            DoOne("countries_patents_remastered", 12);
            //DoOne("countries_education_remastered", 2);
            //Iron.graphic();
        } catch (Exception ex) {
            Logger.getLogger(Iron.class.getName()).log(Level.SEVERE, null, ex);
        }
	}
    private static void jumper() throws FileNotFoundException, CalculationException, IOException, ClassNotFoundException{
            String[] inputSets = new String[]{
                //"countries_general_remastered",
                //"countries_health_remastered",
                "countries_water_remastered",
                "countries_education_remastered",
                //"countries_gdp_remastered",
                //"countries_educationfields_remastered",
                //"countries_patents_remastered",
                //"countries_population_remastered",
                "nme_gdp_remastered",
                "pdm"};
//            for(int i =0; i<inputSets.length; i++){
//                datasets.add(calcDataSet(inputSets[i]));
//            }
//            serialize(datasets, "remastered.txt");
            deserialize("full.txt");
            Util.print(datasets.size());
            printOut(datasets);
            //detail(datasets, 1, 5);
            //detail(datasets, 6, 12);
            //detail(datasets, 3, 2);
    }
    private static void detail(ArrayList<DataSetListItem> datasetlist, int dataset, int dmu) throws CalculationException{
        Util.print(AnalysisHelper.GetObjectHeader());
        //for(DataSetListItem d:datasetlist){
        DataSetListItem d = datasetlist.get(dataset);
        AnalysisHelper helper = new AnalysisHelper(d);
        Util.print(helper.GetObjectToCSVString());
        Util.print(helper.GetExtremeDmuString());

        Util.print("DMU " + dmu);
        Util.print("Alpha;Obj-Heur;Obj-Glob;Objective reached (%);Cnt Glob Optimizers;PD-Heur;PD-Glob;PD reached (%);Eff-Heur;Eff-Glob;Eff reached (%)");
            for(AnalysisItem a: d.dmus.get(dmu).anaList){
                    GlobalPdmOptimization globo = (GlobalPdmOptimization)a.globOptimization;
                    Util.print(a.heurOptimization.getAlpha() + ";"
                            + a.heurOptimization.getOptimizationObjective()+ ";"
                            + a.globOptimization.getOptimizationObjective() + ";"
                            + (a.heurOptimization.getOptimizationObjective()/a.globOptimization.getOptimizationObjective()*100) + ";"
                            + globo.optimizers.size() + ";"
                            + a.heurOptimization.getProfileDistance() + ";"
                            + a.globOptimization.getProfileDistance() + ";"
                            + (a.heurOptimization.getProfileDistance()/a.globOptimization.getProfileDistance()*100) + ";"
                            + a.heurOptimization.getEfficiency() + ";"
                            + a.globOptimization.getEfficiency() + ";"
                            + (a.heurOptimization.getEfficiency()/a.globOptimization.getEfficiency()*100));
                }
//                AnalysisItem a = d.dmus.get(dmu).anaList.get(0);
//                    GlobalPdmOptimization globo = (GlobalPdmOptimization)a.globOptimization;
//                    Util.print(a.heurOptimization.getAlpha() + ";"
//                            + a.heurOptimization.getOptimizationObjective()+ ";"
//                            + a.globOptimization.getOptimizationObjective() + ";"
//                            + (a.heurOptimization.getOptimizationObjective()/a.globOptimization.getOptimizationObjective()*100) + ";"
//                            + globo.optimizers.size() + ";"
//                            + a.heurOptimization.getProfileDistance() + ";"
//                            + a.globOptimization.getProfileDistance() + ";"
//                            + (a.heurOptimization.getProfileDistance()/a.globOptimization.getProfileDistance()*100) + ";"
//                            + a.heurOptimization.getEfficiency() + ";"
//                            + a.globOptimization.getEfficiency() + ";"
//                            + (a.heurOptimization.getEfficiency()/a.globOptimization.getEfficiency()*100));
//                    for(PdmOptimization op:globo.optimizers){
//                        Util.print(Arrays.toString(op.getOptimizationDirections()));
//                    }
                
        
    }
    private static void DoOne(String filename, int dmu) throws NumberFormatException, CalculationException, IOException{
        DmuList dmus = Util.LoadFile("input\\" + filename + ".csv");
        double[] urmWeights = getUrmWeights(dmus.outputSize);
        Util.print("URM vector: " + Arrays.toString(urmWeights));

    /*    GlobalSolver globSolv = new GlobalSolver(dmus, dmu, urmWeights);
        GlobalPdmOptimization globOpt = (GlobalPdmOptimization)globSolv.getOptimization(0);
         Util.print("done " + globOpt.optimizers.size());
        for(PdmOptimization op:globOpt.optimizers){
                        Util.print(Arrays.toString(op.getOptimizationDirections()));
                    }*/


        double alpha = 0d;//103.80346052662200d;
        //Bisection_1 heur = new Bisection_1(dmus, dmu, urmWeights, Bisection_1.SolverModell.Heuristik, 0.1E-8);
        //List<PdmOptimization> heurOpts = diaHelper(heur.getTransitions());
//        Optimization deaOpt = new Optimization(dmus, dmu);
//        Optimizer.calculateDea(deaOpt);
//        Util.print("----------DEA-----------");
//        Util.print(deaOpt.getOptimizationObjective() + ";"
//                + deaOpt.getEfficiency());
//        Util.print(Arrays.toString(deaOpt.getOptimizationOutputWeights()));
//
//        Util.print("----------PDM-----------");
//        PdmOptimization heur = new PdmOptimization(dmus, dmu, alpha, urmWeights, deaOpt);
//        Optimizer.calculatePdm(heur);
//        Util.print("URM relative: " + Arrays.toString(heur.getRelUrmWeights()));
//        Util.print("Alpha: " + alpha);
//       Util.print(heur.getOptimizationObjective() + ";"
//                + heur.getEfficiency() + ";"
//                + heur.getProfileDistance() + ";");
//       Util.print(Arrays.toString(heur.getOptimizationOutputWeights()));
//       Util.print(Arrays.toString(heur.getOptimizationDirections()));
//       Util.print("_______________");
//
//       GlobalSolver globSolv = new GlobalSolver(dmus, dmu, urmWeights);
//       GlobalPdmOptimization globOpt = (GlobalPdmOptimization)globSolv.getOptimization(alpha);
//       Util.print("Global: " + (globOpt.optimizers.size()-1));
//       //for(PdmOptimization g : globOpt.optimizers){
//           PdmOptimization g = globOpt.optimizers.get(0);
//            Util.print(g.getOptimizationObjective() + ";"
//                + g.getEfficiency() + ";"
//                + g.getProfileDistance() + ";");
//            Util.print(Arrays.toString(g.getOptimizationOutputWeights()));
//            Util.print(Arrays.toString(g.getRelOptimizationWeights()));
//            Util.print(Arrays.toString(g.getOptimizationDirections()));
//            Util.print("_______________");
       //}


      

       Util.print("Alpha;Obj-Heur;Obj-Glob;Objective reached (%);Cnt Glob Optimizers;Cnt different Optimizers;PD-Heur;PD-Glob;PD reached (%);Eff-Heur;Eff-Glob;Eff reached (%)");
        //List<PdmOptimization> heurOpts = heur.getTransitions();
        //Util.print("Found transitions: " + heurOpts.size());
        GlobalSolver globSolv = new GlobalSolver(dmus, dmu, urmWeights);
//        for (PdmOptimization a : heurOpts){
//            GlobalPdmOptimization globo = (GlobalPdmOptimization)globSolv.getOptimization(a.getAlpha());
//                    Util.print(a.getAlpha() + ";"
//                            + a.getOptimizationObjective()+ ";"
//                            + globo.getOptimizationObjective() + ";"
//                            + (a.getOptimizationObjective()/globo.getOptimizationObjective()*100) + ";"
//                            + globo.optimizers.size() + ";"
//                            + Tool.getDifferentOptimizers(globo.optimizers) + ";"
//                            + a.getProfileDistance() + ";"
//                            + globo.getProfileDistance() + ";"
//                            + (a.getProfileDistance()/globo.getProfileDistance()*100) + ";"
//                            + a.getEfficiency() + ";"
//                            + globo.getEfficiency() + ";"
//                            + (a.getEfficiency()/globo.getEfficiency()*100));
//       }
        alpha = 0.671557382680475;
        HeuristikSolver heurSolv = new HeuristikSolver(dmus, dmu, urmWeights);
        GlobalPdmOptimization globo = (GlobalPdmOptimization)globSolv.getOptimization(alpha);
        PdmOptimization heurOpt = heurSolv.getOptimization(alpha);

                    Util.print("Diffs: " + Tool.getDifferentOptimizers(globo.optimizers));

                    /*for(PdmOptimization op:globo.optimizers){
                        Util.print(op.getOptimizationObjective() + " -- " + op.getEfficiency());
                    }*/
                    for(PdmOptimization op:globo.optimizers){
                        Util.print(Arrays.toString(op.getRelOptimizationWeights()));
                    }
                    //Util.print(Arrays.toString(heurOpt.getRelOptimizationWeights()));
                    //Util.print(Arrays.toString(globo.getRelUrmWeights()));
                    //Util.print(globo.optimizers.size());
    }
    private static List<PdmOptimization> diaHelper(List<PdmOptimization> list) throws CalculationException{
        for(int i = 1; i< list.size()-1;i++){
            if(list.get(i).getAlpha() == list.get(i-1).getAlpha()){
                list.get(i).setOptimizationObjective(list.get(i+1).getOptimizationObjective());
                list.get(i).setProfileDistance(list.get(i+1).getProfileDistance());
            }
        }
        return list;
    }
    private static void deserialize(String filename) throws FileNotFoundException, IOException, ClassNotFoundException {
        Util.print("Starting deserialization " + Util.getTimeStamp());
        datasets = null;
		FileInputStream fis = null;
		ObjectInputStream in = null;
		fis = new FileInputStream(filename);
		in = new ObjectInputStream(fis);
		datasets = (ArrayList<DataSetListItem>) in.readObject();
		in.close();
        Util.print("Finished deserialization " + Util.getTimeStamp());
    }
    private static void serialize(ArrayList<DataSetListItem> datasetlist, String filename){

        FileOutputStream fos = null;
		ObjectOutputStream out = null;
		try {
			fos = new FileOutputStream(filename);
			out = new ObjectOutputStream(fos);
			out.writeObject(datasetlist);
			out.close();
			System.out.println("Object Persisted");
		} catch (IOException ex) {
			ex.printStackTrace();
		}

    }
    private static void printOut(ArrayList<DataSetListItem> datasetlist) throws CalculationException{
        Util.print(AnalysisHelper.GetObjectHeader());
        for(DataSetListItem d:datasetlist){
//            if(d.dmus.size() == 29){
            Util.print("DATASET: " + d.datasetname);
            //AnalysisHelper helper = new AnalysisHelper(d);
            //Util.print(helper.GetObjectToCSVString());
            //Util.print(helper.GetExtremeDmuString());
            for(DmuListItem dmu : d.dmus){
                Util.print("\tDMU: " + dmu.dmu);
                //Util.print("Alpha;HeurObj;GlobObj;HeurDist;GlobDist");
                for(AnalysisItem a:dmu.anaList){
                    int diffs = Tool.getDifferentOptimizers(((GlobalPdmOptimization)a.globOptimization).optimizers);
                    if(diffs > 1){
                        Util.print(a.globOptimization.getAlpha() + ";" + diffs);
                    }
                    //Util.print(a.heurOptimization.getAlpha() + ";" + a.heurOptimization.getOptimizationObjective()+ ";" + a.globOptimization.getOptimizationObjective() + ";" + a.heurOptimization.getProfileDistance() +";" + a.globOptimization.getProfileDistance());
                }
            }
//            }

        }

    }
    private static double[] getUrmWeights(int cnt){
            double[] urmWeights = new double[cnt];

            for(int i = 0; i< cnt; i++){
                urmWeights[i] = weightVector[i];
            }

            return urmWeights;
    }
    public static DataSetListItem calcDataSet(String filename) throws NumberFormatException, CalculationException, IOException{
        Util.print("CalcDataSet " + filename + " - " + Util.getTimeStamp());
        DataSetListItem curr = new DataSetListItem(filename);
        DmuList dmus = Util.LoadFile("input\\" + filename + ".csv");
        double[] urmWeights = getUrmWeights(dmus.outputSize);
        for(int i=0;i<dmus.size();i++){
            Util.print("\tDMU " + i + " - " + Util.getTimeStamp());
            DmuListItem dmu = new DmuListItem(i);
            Bisection_1 heur = new Bisection_1(dmus, i, urmWeights, Bisection_1.SolverModell.Heuristik, 0.1E-5);
            List<PdmOptimization> heurOpts = heur.getTransitions();
            Util.print("\tFOUND TRANSITIONS: " + heurOpts.size());
            Util.print("\t\tSTEP 1 - " + Util.getTimeStamp());
            GlobalSolver globSolv = new GlobalSolver(dmus, i, urmWeights);
            int x=0;
            for (PdmOptimization heurOpt : heurOpts){
                dmu.anaList.add(new AnalysisItem(heurOpt, globSolv.getOptimization(heurOpt.getAlpha())));
                Util.print("\t\tSTEP 2." + ++x + " - " + Util.getTimeStamp());
            }
            curr.dmus.add(dmu);
        }
        return curr;
    }
    /*public static void test(String filename, int selectedDmu) throws NumberFormatException, IOException, CalculationException{
        double[] urmWeights = new double[]{21, 34, 11, 13, 15, 22, 32, 21, 34, 11, 13, 15, 22, 32};
        urmWeights = new double[]{21, 34, 11, 13, 15};
        //urmWeights = new double[]{21, 34, 11, 13, 15, 22, 32, 21};
        //urmWeights = new double[]{21, 34, 11, 13, 15, 22};
        //urmWeights = new double[]{21, 34, 11, 13, 15, 22, 32, 21, 34, 11, 13, 15, 22, 32, 21, 34, 11, 13, 15, 22, 32, 21, 34, 11, 13, 15, 22, 32, 15, 22, 32};
        DmuList dmus = Util.LoadFile("input\\" + filename + ".csv");

        Util.initializeFileWriter("output\\" + filename + "_" + selectedDmu + ".txt");

        Bisection b = new Bisection(dmus, selectedDmu, urmWeights, Bisection.SolverModell.Heuristik, 100);
        List<PdmOptimization> opts = b.getTransitions();
        double[] xAxis = new double[opts.size()*2];
        double[] yAxis = new double[opts.size()*2];
        int cnt = 0;
        int ind = 0;
        for(PdmOptimization o:opts){
            try {
                Util.print("alpha " + o.getAlpha() + " " + o.getProfileDistance());
                xAxis[cnt] = ind;//o.getAlpha();
                xAxis[cnt+1] = ind+1;
                yAxis[cnt] = o.getProfileDistance();
                yAxis[cnt+1] = o.getProfileDistance();
                ind++;
                cnt+=2;
            } catch (CalculationException ex) {
                Logger.getLogger(Iron.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        Plot2D plot = new SimplePlotXY(xAxis, yAxis, null, "Alpha", "ProfileDistance", null, null);//, new XSymbol());

        //PlotAxis xas = plot.getHorizontalAxis();
		//xas.setScale(new Log10AxisScale());

        PlotPanel panel = new PlotPanel(plot);
		panel.setBackground( Color.white );

        PlotWindow window = new PlotWindow("PlotXY Plot Window", panel);
		window.setSize(500, 300);
		window.setLocation(50,50);
		window.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		window.show();
    }*/

            /**
         * LOGFORMAT
         * [ALPHA}:
         * [HEURISTIK_OBJ];[HEURISTIK_DIRECTION];[HEURISTIK_OPTIMIERER];
         * [BEST_OBJ];[BEST_DIRECTION_X];[BEST_DIRECTION_X_OPTIMIERER]
         * [BEST_OBJ];[BEST_DIRECTION_X+1];[BEST_DIRECTION_X+1_OPTIMIERER]
         * [BEST_OBJ];[BEST_DIRECTION_X+n];[BEST_DIRECTION_X+n_OPTIMIERER]
         * .
         * .
         * .
         * [ALPHA]
         *
         *
         * Calculate DEA
         * Get Heuristik Directions
         *
         * for all alphasteps
         *
         * print alpha;
         *
            * Calculate PDM using Heuristik Directions
         *      print OBJ.Value Heuristik;
         *
            * Calculate all other Directions to find Best Directions (best objective)
         *      print best OBJ.Value;
         *      print anzahl best OBJ. Optimierer
         *
         */

    private static void doIt(){
        

    }
    public static void dataAnalysis(String filename, int selectedDmu) throws IOException, CalculationException{
        double[] urmWeights = new double[]{21, 34, 11, 13, 15, 22, 32, 21};
        int steps =200;
        DmuList dmus = Util.LoadFile("input\\" + filename + ".csv");

        Util.initializeFileWriter("output\\" + filename + "_" + selectedDmu + ".txt");
        Util.log("Init DMU" + selectedDmu);

        Optimization deaOpt = new Optimization(dmus, selectedDmu);
        Optimizer.calculateDea(deaOpt);
        PdmOptimization pdmOpt = new PdmOptimization(dmus,selectedDmu, 0, urmWeights, deaOpt);
        Optimizer.calculatePdm(pdmOpt);

        //get heuristik directions
        int[] heuristikDirections = pdmOpt.getOptimizationDirections();
        //get upper alpha bound
        Bisection biSec = new Bisection(dmus, selectedDmu,urmWeights, Bisection.SolverModell.Heuristik, 100);
        List<PdmOptimization> pdmList = biSec.getTransitions(2);
        for(PdmOptimization o:pdmList){
                Util.print("alpha " + o.getAlpha() + " " + o.getProfileDistance() + o.getOptimizationObjective());
        }
        double alphaUpperBound = pdmList.get(pdmList.size()-1).getAlpha()*1.2;
        double alphastep = alphaUpperBound/steps;
        Util.log("LowBo: 0  UppBo: " + alphaUpperBound + "  Stepsize: " + alphastep);

        String line;
        for(int i =34;i<35/*steps*/;i++){
            //calculate Heuristik and print
            double alpha = alphastep*i;
            Util.log("ALPHA" + i + ": " + alpha);
            line = alpha + ";";

            PdmOptimization heur = new PdmOptimization(dmus, selectedDmu, alpha, urmWeights, heuristikDirections);
            Optimizer.calculatePdm(heur);
            line += heur.getOptimizationObjective() + ";";
            Util.log("URM:");
            Util.log((Arrays.toString(Iron.getRelWeights(urmWeights))));
            Util.log("Heuristik:");
            Util.log(Arrays.toString(Iron.getRelWeights(heur.getOptimizationOutputWeights())) + ";" + heur.getOptimizationObjective() + ";" + heur.getProfileDistance()+ ";" + Arrays.toString(heur.getOptimizationDirections()) + ";");

            List<PdmOptimization> pdmBest = Tool.getBestObjectiveDirections(dmus, selectedDmu, urmWeights, alpha);
            line += pdmBest.get(0).getOptimizationObjective() + ";";
            line += pdmBest.size() + ";";
            Util.log("Matching Best Objectives: " + pdmBest.size());
            for(PdmOptimization p:pdmBest){
                Util.log(Arrays.toString(p.getOptimizationOutputWeights()) + ";"+ p.getOptimizationObjective() + ";" + p.getProfileDistance() + ";"+ Arrays.toString(p.getOptimizationDirections()) + ";");
            }

            Util.writeToFile(line);
            Util.log("_______");
        }
        Util.closeFileWriter();
    }
    private static void et(String filename, int selectedDmu) throws IOException, CalculationException{
        double alpha = 3.7997961044311526E-10;
        double[] urmWeights = new double[]{21, 34, 11, 13, 15, 22, 32, 21};
        DmuList dmus = Util.LoadFile("input\\" + filename + ".csv");
        int[] dir = new int[]{-1,1,-1,-1,1,1,1,1};

        PdmOptimization pdmOpt = new PdmOptimization(dmus, selectedDmu, alpha, urmWeights, dir);
        Optimizer.calculatePdm(pdmOpt);
        Util.log(Arrays.toString(Iron.getRelWeights(pdmOpt.getOptimizationOutputWeights())) + ";"+ pdmOpt.getOptimizationObjective() + ";" + Arrays.toString(pdmOpt.getOptimizationDirections()) + ";");

        dir = new int[]{1,1,-1,-1,-1,1,1,1};

        pdmOpt = new PdmOptimization(dmus, selectedDmu, alpha, urmWeights, dir);
        Optimizer.calculatePdm(pdmOpt);
        Util.log(Arrays.toString(Iron.getRelWeights(pdmOpt.getOptimizationOutputWeights())) + ";"+ pdmOpt.getOptimizationObjective() + ";" + Arrays.toString(pdmOpt.getOptimizationDirections()) + ";");

    }
    	public static double[] getRelWeights(double[] absUrmWeights) {
		double sum = 0;
        double[] ret = new double[absUrmWeights.length];
		for(int x = 0;x < absUrmWeights.length;x++){
			ret[x] = absUrmWeights[x]/absUrmWeights[0];
		}
		return ret;
	}
    public static void ah() throws IOException, CalculationException{
        try {
            double[] urmWeights = new double[]{21, 34, 11, 13, 15, 22, 32, 21};
            //double[] urmWeights = new double[]{12, 20, 7, 8, 9, 13, 19, 12};
            DmuList u = util.Util.LoadFile("PDM1.csv");
            Optimization deaOpt = new Optimization(u, 2);
            Optimizer.calculateDea(deaOpt);
            PdmOptimization pdmOpt = new PdmOptimization(u, 2, 0, urmWeights, deaOpt);
            Util.print(Arrays.toString(pdmOpt.getRelUrmWeights()));
            Util.print(Arrays.toString(pdmOpt.getOptimizationDirections()));
            Optimizer.calculatePdm(pdmOpt);
			//int[] heuristikDirections = pdmOpt.getOptimizationDirections();


            TransitionExplorer expl = 
                    new TransitionExplorer
                    (deaOpt,
                    urmWeights,
                    new PdmOptimization(u, deaOpt.getObjectiveDmu(), 0,urmWeights, deaOpt).getOptimizationDirections());
            for(PdmOptimization o:expl.getTransitionList()){
                Util.print(o.getAlpha() + "\t \t\t" + o.getProfileDistance());
            }

        } catch (CalculationException ex) {
            Logger.getLogger(Iron.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NumberFormatException ex) {
            Logger.getLogger(Iron.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Iron.class.getName()).log(Level.SEVERE, null, ex);
        }



    }
	public static void analyze() {
		DmuList u;
		String filename = "beasley.csv";
		
		try {
			double[] urmWeights = new double[]{21, 34, 11, 13, 15, 22, 32 ,21};
			
			u = util.Util.LoadFile(filename);
			
			for(int a = 2;a<u.size();a++){
				double alpha = 0.0;
				
				//prepare Heuristik directions
				Optimization deaOpt = new Optimization(u, a);
				Optimizer.calculateDea(deaOpt);
				PdmOptimization pdmOpt = new PdmOptimization(u, a, alpha, urmWeights, deaOpt);
				Util.log("UrmWeights: " + Arrays.toString(pdmOpt.getRelUrmWeights()));
				Optimizer.calculatePdm(pdmOpt);
				int[] heuristikDirections = pdmOpt.getOptimizationDirections();
				
				
				//Util.print(pdmOpt.toString());
				
				//get upper bound for alpha
				TransitionExplorer expl = new TransitionExplorer(deaOpt, urmWeights, pdmOpt.getOptimizationDirections());
				List<PdmOptimization> list = expl.getTransitionList();
				for(PdmOptimization p:list){
					Util.log("alpha:" + p.getAlpha() + " - " + p.toString());
				}
				
				//input variables
				int iterations = 300;
				double step = (list.get(list.size()-1).getAlpha()+(list.get(list.size()-1).getAlpha()*0.1))/iterations;
				alpha = 0;
				
				//initialize filewriter
				String header = "Alpha;Anzahl;ObjectMax;ObjectHeur;MinAbstandHeur;MaxAbstandHeur;MinAbstandOpt;MaxAbstandOpt";
				Util.initializeFileWriter("RESULT_" + filename.replace('.', '_')+ "_DMU_" + a + ".csv");
				Util.writeToFile(header);
				
				
				
				String line;
				for(int i = 0;i<=iterations;i++){
					alpha = alpha + step;
					Util.log(i + " ALPHA: " + alpha);
					line = alpha +";";
					
					PdmOptimization heuristik = new PdmOptimization(u, a, alpha, urmWeights, heuristikDirections);
					Optimizer.calculatePdm(heuristik);
					List<PdmOptimization> optima = null;
					
					line += optima.size()+";" + optima.get(0).getOptimizationObjective() + ";" + heuristik.getOptimizationObjective() + ";";
					
					ArrayList<Double> externals = Tool.getDiffsToExternalVector(optima, heuristik);
					line+= externals.get(0) + ";" + externals.get(externals.size()-1);
					
					if(optima.size()>1){
						ArrayList<Double> internals = Tool.getInternalVectorDiffs(optima);
						line+= internals.get(0) + ";" + internals.get(internals.size()-1)+ ";";
					}
					
					Util.writeToFile(line);
				}
				Util.closeFileWriter();
			}
			
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (CalculationException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
